package com.example.lcode.list;

import java.util.List;

public class Question19 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode firstNode = head;
        ListNode secondNode = head;
        for (int i = 0; i < n ; i++) {
            secondNode = secondNode.next;
        }
        if (secondNode == null) {
            return head.next;
        }
        while (secondNode.next != null) {
            secondNode = secondNode.next;
            firstNode = firstNode.next;
        }
        firstNode.next = firstNode.next.next;
        return head;
    }

    public static void main(String[] args) {

    }

}
